//
//  FileLoger.m
//  FileLoger
//
//  Created by 水石 温 on 11-11-16.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "FileLoger.h"

@implementation FileLoger
@synthesize handle, dateFormatter, path;

-(id)init {
    self=[super init];
    NSString *path1 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:path1]) {
		[[NSFileManager defaultManager] createDirectoryAtPath:path1 withIntermediateDirectories:YES attributes:nil error:nil];
	}
	NSString *filePath = [path1 stringByAppendingPathComponent:@"log"];
    NSString *logFilePath = filePath;
    //logFilePath=[logFilePath stringByAppendingPathComponent:@"log"];
    //logFilePath=@"/Users/wenshuishi/log/";
    //NSLog(@"log file path:%@",logFilePath);
    self.dateFormatter = [[NSDateFormatter alloc] init];
    [self.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:logFilePath]) {
		[fm createDirectoryAtPath:logFilePath withIntermediateDirectories:YES attributes:nil error:nil];
	}
	path = [logFilePath stringByAppendingPathComponent:@"iPhoneRec.xml"];
    
    if ([fm fileExistsAtPath:path]) {
        NSDictionary *attrs=[fm attributesOfItemAtPath:path error:nil];
        NSInteger fileSize= [[attrs objectForKey:NSFileSize] intValue];
//            NSLog(@"log file size: %d bytes",fileSize);
        printf("log file size: %d bytes \n ", fileSize );
        if (fileSize > 1024*128) {
            [fm removeItemAtPath:path error:nil];
            [fm createFileAtPath:path contents:nil attributes:nil];
            //        NSLog(@"log file is large than 100KB, del log file, size: %d bytes",fileSize);
        }
    } else {
        [fm createFileAtPath:path contents:nil attributes:nil];
        //    NSLog(@"log file not exist,create new log file");
    }
    
    self.handle = [NSFileHandle fileHandleForWritingAtPath:path];
    [self.handle seekToEndOfFile];
    return self;
}


+(FileLoger*)share {
    static FileLoger *loger=nil;
    if (loger==nil) {
        loger = [[FileLoger alloc] init];
    }
    return loger;
}

-(void)log:(NSString*)format,... {
    @autoreleasepool {
        @synchronized(self) {
            va_list args;
            va_start(args, format);
            NSString *newFormat=[NSString stringWithFormat:@"%@:%@\n",[self.dateFormatter stringFromDate:[NSDate date]],format];
            NSString *content = [[NSString alloc] initWithFormat:newFormat arguments:args];
            va_end(args);
            [handle writeData:[content dataUsingEncoding:NSUTF8StringEncoding]];
        }
    }
}

//-(NSString*)compressLogFile {
//    @synchronized(self) {
//        NSFileManager *fm=[NSFileManager defaultManager];
//        
//        if (![fm fileExistsAtPath:path]) {
//            return nil;
//        }
//        NSString *zipPath=[NSFileManager storePathWithFileName:@"log.zip"];
//        [fm removeItemAtPath:zipPath error:nil];
//        [fm createFileAtPath:zipPath contents:nil attributes:nil];
//        
//        
//        ZipArchive *zip=[[ZipArchive new] autorelease];
//        
//        BOOL result=[zip CreateZipFile2:zipPath];
//        NSLog(@"create log zip file:%@  %@",zipPath, result== YES ? @"success" : @"fail");
//        [zip addFileToZip:path newname:[[path componentsSeparatedByString:@"/"] lastObject]];
//        [zip CloseZipFile2];
//        NSLog(@"compress to zip file finish");
//        return zipPath;
//    }
//}


-(void)dealloc {
    [self.handle closeFile];
}

@end
